package com.dzj.fishingcommunity.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dzj.fishingcommunity.common.ResponseResult;
import com.dzj.fishingcommunity.model.CommentContent;
import com.dzj.fishingcommunity.service.CommentContentService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/comment-contents")
@Tag(name = "评论内容管理", description = "评论内容管理相关的接口")
public class CommentContentController {

    @Autowired
    private CommentContentService commentContentService;

    @Operation(summary = "获取所有评论内容")
    @GetMapping
    public ResponseResult<List<CommentContent>> getAllCommentContents(
            @Parameter(description = "当前页码", required = false) @RequestParam(defaultValue = "1") int current,
            @Parameter(description = "每页大小", required = false) @RequestParam(defaultValue = "10") int size) {
        Page<CommentContent> page = new Page<>(current, size);
        Page<CommentContent> commentContentPage = commentContentService.page(page);
        return ResponseResult.success(commentContentPage.getRecords());
    }

    @Operation(summary = "根据ID获取评论内容")
    @GetMapping("/{id}")
    public ResponseResult<CommentContent> getCommentContentById(@PathVariable Integer id) {
        CommentContent commentContent = commentContentService.getById(id);
        return ResponseResult.success(commentContent);
    }

    @Operation(summary = "新增评论内容")
    @PostMapping
    public ResponseResult<CommentContent> addCommentContent(@RequestBody CommentContent commentContent) {
        commentContentService.save(commentContent);
        return ResponseResult.success(commentContent);
    }

    @Operation(summary = "更新评论内容")
    @PutMapping("/{id}")
    public ResponseResult<CommentContent> updateCommentContent(@PathVariable Integer id, @RequestBody CommentContent commentContent) {
        commentContent.setCommentIndexId(id);
        commentContentService.updateById(commentContent);
        return ResponseResult.success(commentContent);
    }

    @Operation(summary = "删除评论内容")
    @DeleteMapping("/{id}")
    public ResponseResult<Void> deleteCommentContent(@PathVariable Integer id) {
        boolean isDeleted = commentContentService.removeById(id);
        if (!isDeleted) {
            return ResponseResult.error(400, "评论内容删除出现异常");
        }
        return ResponseResult.success(null);
    }
}